<?php

/**
 * Anthor : xushuai
 * Date : 20140-7-*
 */

namespace Xuan\Controller;

use Think\Controller;

class CommonController extends Controller {

    public function _initialize() {
        $s_admin_uid = session("admin_uid")?session("admin_uid"):cookie("admin_uid");
        if(session("admin_uid") == ''){
            session("admin_uid",$s_admin_uid);
        }        
        $s_admin_name = session("admin_name")?session("admin_name"):cookie("admin_name");
        if(session("admin_name") == ''){
            session("admin_name",$s_admin_name);
        }
        if ($s_admin_uid == '') {
            echo "<script>document.location.href='" . U("Public/login") ."';</script>";
        }
        $control = CONTROLLER_NAME;
        $mod = ACTION_NAME;
        $modReturn = array("lists", "cat", "label", "unit", "draw", "single", "circle");
        if (in_array($mod, $modReturn)) {
            $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            session('QUERY_STRING', $url);
        }
        /******菜单权限*****/
        $info = M("admin_user")->field("action,id")->where("id = ".$s_admin_uid."")->find();
        
        if($info['id'] != 1){
             $sql_pids = " AND id in (" . $info['action'] . ")"; 
             $sql_ids = " AND id in (" . $info['action'] . ")"; 
        }
        /******菜单权限over*****/
        $menus = M("admin_menu2")->where("pid = 0 AND is_check = 1 ".$sql_pids."")->order("ord ASC")->select();
        foreach ($menus as $k => $v) {
            $menus[$k]['url'] = U($v['control'] . "/" . $v['mod']);
            $menus[$k]['sub'] = M("admin_menu2")->where("pid = " . $v['id'] . " AND is_check = 1 ".$sql_ids."")->order("ord ASC")->select();
            foreach ($menus[$k]['sub'] as $k2 => $v2) {
             
                $menus[$k]['sub'][$k2]['url'] =__APP__."/".MODULE_NAME."/".ucfirst($v2['control'])."/".strtolower($v2['mod']).$v2['url_other'];
            }
        }
    
        $menu = M("admin_menu2")->field("pid")->where("control = '" . $control . "' AND `mod` = '" . $mod . "'")->find();
        $three =getTableFile('three');
       $this->assign("three",  $three[0]);
        $this->assign("config", getTableConfig());
        $this->assign("control", $control);
        $this->assign("mod", $mod);
        $this->assign("menus", $menus);
        $this->assign("menu_pid", $menu['pid']);
        $this->assign("s_admin_name", $s_admin_name);
    }

}
